package academic.ui.client.view;


import ui.client.GenericClickable;
import ui.client.GenericContainerTag;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.ViewImpl;


import academic.ui.client.config.CSSConstants;
import academic.ui.client.presenter.MyAccountPresenter;
import academic.ui.client.util.ClientHelper;
import ecommerce.soco.shared.GWT_Customer;


public class MyAccountView extends ViewImpl implements MyAccountPresenter.MyView {

	GenericContainerTag accountDiv = new GenericContainerTag("div");
	
	//Avatar Fields
	Image img = new Image();
	GenericClickable changeAvatar = new GenericClickable("a","Modify");
	
	//Name Fields
	GenericContainerTag nameSpan = new GenericContainerTag("span");
	GenericClickable updateName = new GenericClickable("a","Modify");
	
	//email fields
	GenericContainerTag emailSpan = new GenericContainerTag("span");
	GenericClickable updateEmail = new GenericClickable("a","Modify");
	
	//Password fields
	GenericClickable updatePasswd = new GenericClickable("a","Modify");
	GenericContainerTag passwdEdit = new GenericContainerTag("span");
	PasswordTextBox currentPasswd = new PasswordTextBox();
	TextBox newPasswd = new PasswordTextBox();
	Button submitPasswdButton = new Button("save");
	
	
	//card div
	GenericContainerTag cardDiv = new GenericContainerTag("div");
	
	Hidden customerID = new Hidden();
	
	TextBox nameField = new TextBox();
	GenericContainerTag nameEdit = new GenericContainerTag("span");
	Button submitNameButton = new Button("save");
	
	TextBox emailField = new TextBox();
	GenericContainerTag emailEdit = new GenericContainerTag("span");
	Button submitEmailButton = new Button("save");
	
	GenericContainerTag passwdSpan = new GenericContainerTag("span","*******");
	GenericContainerTag invalidPasswdSpan = new GenericContainerTag("span","invalid password");
	GenericContainerTag changePasswdLabels = new GenericContainerTag("div");
	
	FormPanel form = new FormPanel();
	FileUpload fp = new FileUpload();
	
	@Inject
	public MyAccountView(CSSConstants css) {
		accountDiv.setStyleName("accountDiv");
		GenericContainerTag avatarDiv = new GenericContainerTag("div");
		avatarDiv.setStyleName("avatarDiv");
		accountDiv.add(avatarDiv);
		GenericContainerTag avatarContent = new GenericContainerTag("div");
		avatarContent.setStyleName("accountContent");
		GenericContainerTag avatarSpan = new GenericContainerTag("span");
		avatarSpan.setStyleName("accountSpan");
		
		changePasswdLabels.add(new GenericContainerTag("div","Current Password"));
		changePasswdLabels.add(new GenericContainerTag("div","New Password"));
		avatarDiv.add(new GenericContainerTag("label","My Photo:"));
		avatarDiv.add(avatarContent);
		avatarContent.add(avatarSpan);
		img.setStyleName("hundByhundPhoto");
		avatarSpan.add(img);
		avatarContent.add(changeAvatar);
		
		
		
		form.clear();
		form.setEncoding(FormPanel.ENCODING_MULTIPART);
		form.setMethod(FormPanel.METHOD_POST);
		form.setStyleName("uploadImageForm");
		form.setVisible(false);
		fp.setName("upload");
		Button submitForm = new Button("upload file",new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				form.submit();
			}
			
		});
		Button cancelSubmit = new Button("cancel",new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				form.setVisible(false);
			}
			
		});
		
		changeAvatar.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				form.setVisible(true);
			}
			
		});
		GenericContainerTag formContent = new GenericContainerTag("div");
		formContent.add(fp);
		formContent.add(customerID);
		formContent.add(submitForm);
		formContent.add(cancelSubmit);
		customerID.setName("customerID");
		
		form.setAction("/upload");
		form.add(formContent);
		avatarContent.add(form);
		
		GenericContainerTag nameDiv = new GenericContainerTag("div");
		nameDiv.setStyleName("nameDiv");
		accountDiv.add(nameDiv);
		GenericContainerTag nameContent = new GenericContainerTag("div");
		nameContent.setStyleName("nameContent");
		nameSpan.setStyleName("accountSpan");
		nameContent.add(nameSpan);
		nameDiv.add(new GenericContainerTag("label","Name:"));
		nameDiv.add(nameContent);
		nameContent.add(nameEdit);
		nameContent.add(updateName);	
		
		nameEdit.setStyleName("accountSpan heightSixty");
		nameEdit.add(nameField);
		nameEdit.setVisible(false);
		emailEdit.setStyleName("accountSpan heightSixty");
		emailEdit.add(emailField);
		emailEdit.setVisible(false);
		passwdEdit.setStyleName("accountSpan heightSixty");
		passwdEdit.setVisible(false);
		invalidPasswdSpan.setStyleName("invalidPasswdSpan");
		passwdEdit.add(changePasswdLabels);
		changePasswdLabels.setStyleName("myAccntPasswdLabels");
		passwdEdit.add(invalidPasswdSpan);
		invalidPasswdSpan.setVisible(false);
		passwdEdit.add(currentPasswd);
		passwdEdit.add(newPasswd);
		
		updateName.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				nameEdit.setVisible(true);
				nameField.setText(nameSpan.getElement().getInnerHTML());
				nameSpan.setVisible(false);
			}
		});
		updateEmail.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				emailEdit.setVisible(true);
				emailField.setText(emailSpan.getElement().getInnerHTML());
				emailSpan.setVisible(false);
			}
			
		});
		
		
		
		GenericContainerTag nameButtonsDiv = new GenericContainerTag("div");
		nameEdit.add(nameButtonsDiv);
		nameButtonsDiv.add(submitNameButton);
		Button cancelNameEditButton = new Button("cancel",new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				nameEdit.setVisible(false);
				nameSpan.setVisible(true);
			}
			
		});
		nameButtonsDiv.add(cancelNameEditButton );
		
		GenericContainerTag emailButtonsDiv = new GenericContainerTag("div");
		emailEdit.add(emailButtonsDiv);
		emailButtonsDiv.add(submitEmailButton);
		Button cancelEmailEditButton = new Button("cancel",new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				emailEdit.setVisible(false);
				emailSpan.setVisible(true);
			}
			
		});
		emailButtonsDiv.add(cancelEmailEditButton);
		GenericContainerTag emailDiv = new GenericContainerTag("div");
		emailDiv.setStyleName("accountEmailDiv");
		accountDiv.add(emailDiv);
		emailDiv.add(new GenericContainerTag("label","Email Addresses:"));
		GenericContainerTag emailContent = new GenericContainerTag("div");
		emailContent.setStyleName("emailContent");
		emailSpan.setStyleName("accountSpan");
		emailContent.add(emailSpan);
		emailContent.add(emailEdit);
		emailContent.add(updateEmail);
		emailDiv.add(emailContent);
		
		
		
		
		GenericContainerTag passwdDiv = new GenericContainerTag("div");
		passwdDiv.setStyleName("accountPasswdDiv");
		accountDiv.add(passwdDiv);
		passwdDiv.add(new GenericContainerTag("label","Password:"));
		GenericContainerTag passwdContent = new GenericContainerTag("div");
		passwdContent.setStyleName("passwdContent");
		passwdSpan.setStyleName("accountSpan");
		passwdContent.add(passwdSpan);
		passwdContent.add(passwdEdit);
		passwdContent.add(updatePasswd);
		passwdDiv.add(passwdContent);
		
		
		GenericContainerTag passwdButtonsDiv = new GenericContainerTag("div");
		passwdButtonsDiv.add(submitPasswdButton);
		changePasswdLabels.setVisible(false);
		updatePasswd.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				passwdEdit.setVisible(true);
				changePasswdLabels.setVisible(true);
				invalidPasswdSpan.setVisible(false);
				currentPasswd.setText("");
				newPasswd.setText("");
				passwdSpan.setVisible(false);
			}
			
		});
		passwdButtonsDiv.add(new Button("cancel",new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				passwdEdit.setVisible(false);
				passwdSpan.setVisible(true);
				invalidPasswdSpan.setVisible(false);
			}
		}));
		passwdEdit.add(passwdButtonsDiv);
		
		accountDiv.add(cardDiv);
	}
	
	@Override
	public Widget asWidget() {
		return accountDiv;
	}

	@Override
	public void showMyAccountTab(GWT_Customer customer) {
		if(customer.getPhoto()!=null && !"".equals(customer.getPhoto()))
		{
			img.setUrl(ClientHelper.getImageURL(customer.getPhoto(),customer.getId(),"Users"));
			img.setSize(String.valueOf(100/(customer.getPhotoRatio()>0?customer.getPhotoRatio():1))+"px","100px");
		}
		else
		{
			img.setUrl(GWT.getModuleBaseURL()+"images/blue_ghost.jpg");
			img.setSize("100px", "100px");
		}
		nameSpan.getElement().setInnerHTML(customer.getFirstName()+" "+customer.getLastName());
		nameField.setText(customer.getFirstName()+" "+customer.getLastName());
		
		emailSpan.getElement().setInnerHTML(customer.getEmailAddr());
		
		customerID.setValue(String.valueOf(customer.getId()));
		
	}
	
	@Override
	public void setInSlot(Object slot, Widget w) {
		if(slot==MyAccountPresenter.slot)
		{
			cardDiv.clear();
			cardDiv.add(w);
		}
	}

	@Override
	public HasClickHandlers submitNameChange() {
		return submitNameButton;
	}
	
	@Override
	public HasClickHandlers submitEmailButton() {
		return submitEmailButton;
	}
	
	@Override
	public HasClickHandlers submitPasswdButton() {
		return submitPasswdButton;
	}
	
	
	@Override
	public HasText getNameValue() {
		return nameField;
	}
	
	@Override
	public HasText getEmailValue() {
		return emailField;
	}
	
	@Override
	public HasText getCurrentPasswdValue() {
		return currentPasswd;
	}
	
	@Override
	public HasText getNewPasswdValue() {
		return newPasswd;
	}

	@Override
	public void updatePostNameChanged() {
		nameEdit.setVisible(false);
		nameSpan.setVisible(true);
		nameSpan.getElement().setInnerHTML(nameField.getText());
	}
	
	@Override
	public void updatePostEmailChanged() {
		emailEdit.setVisible(false);
		emailSpan.setVisible(true);
		emailSpan.getElement().setInnerHTML(emailField.getText());
	}
	
	@Override
	public void updatePostPasswdChanged() {
		passwdEdit.setVisible(false);
		passwdSpan.setVisible(true);
	}

	@Override
	public void updatePostPasswdFailure() {
		invalidPasswdSpan.setVisible(true);
		changePasswdLabels.setVisible(false);
	}
	
	@Override
	public FormPanel getUploadForm() {
		return form;
	}

	@Override
	public void updatePhoto(String filename,Double ratio) {
		 img.setUrl(ClientHelper.getImageURL(filename,Integer.parseInt(customerID.getValue()),"Users"));
		 img.setSize(String.valueOf(100/(ratio>0?ratio:1))+"px","100px" );
	}
	
}
